<?php
/**
 * Created by PhpStorm.
 * User: user
 * Date: 2017/8/17
 * Time: 17:07
 */
define("KEY", "4355ec2c7b2011e7ac3c00163e00073b");
define("PARENTER", "2156_854");

define("APPID", "1501837901991004");
define("NAME", "houyuewen");
define("KJMKEY", "lWlhlXCWm3Jol2Rhl2dlaZVw");
class ApplePieAction extends Action{
    //同步商品入口
    public function synchroProductInfo($id,$supplierSkuNo,$vId){
        Session::switchCustomer(0);
        $vendorModel = M("mivendor");
        $vendorInfo = $vendorModel->where("id=$vId")->find();
        $authConfig = json_decode($vendorInfo['authConfig'],true);
        $vendorInfo = array_merge($vendorInfo,$authConfig);
        $disList = $authConfig['distribution'];
        if($vendorInfo['supplierModule']=='weini'){
            $proInfo = R("Weinihaigou/synchroProductInfo",array(trim($supplierSkuNo),$vendorInfo['appid'],$vendorInfo['appkey'],$vendorInfo['synchroUrl']));
            foreach($disList as $key=>$val){
                if($val['deliveryCode']==$proInfo['deliveryCode']){
                    $proInfo['distributionId'] = $val['distributionId'];
                    unset($proInfo['deliveryCode']);
                }
            }
        }else if($vendorInfo['supplierModule']=='kuajingmao'){
            $proInfo = R("CrossBorderCat/synchroProductInfo",array(trim($supplierSkuNo),$vendorInfo['appid'],$vendorInfo['appname'],$vendorInfo['appkey'],$vendorInfo['synchroUrl']));
        }
        if(!$proInfo){
            Output::instance()->resultCode(1,'商品同步失败');
            return;
        }
        $proInfo['fromCustomerId'] = $vendorInfo['cId'];
        $proInfo['vId'] = $vId;
        $proInfo['id'] = $id;
        $_REQUEST['method'] = 'Differ.JH.Business.AddPro';
        $_REQUEST['bizcontent'] = json_encode($proInfo);
        $_REQUEST['appkey']=$vendorInfo['platAppid'];
        $_REQUEST['token']=$vendorInfo['platToken'];
        $unsign     = $vendorInfo['platAppsecret'] . "appkey" . $vendorInfo['platAppid'] . "bizcontent" . json_encode($proInfo) . "method" . 'Differ.JH.Business.AddPro' . "token" . $vendorInfo['platToken'] . $vendorInfo['platAppsecret'];
        $sign    = md5(strtolower($unsign));
        $_REQUEST['sign']=$sign;
        $id = R("Boluopai/index");
        $_REQUEST = null;
        if($id){
            Output::instance()->resultCode();
        }else{
            Output::instance()->resultCode(1,'商品同步失败');
        }
    }
    //同步订单入口
    public function synchroOrder($data){
        $param = json_decode($data['param'],true);
        $oids = $param['oids'];
        Session::switchCustomer(0);
        $orderModel = M("module_product_order");
        $orderList = $orderModel->alias("a")
            ->join("wx_mivendor b on a.vId = b.id")
            ->where("a.id in ($oids) and b.supplierModule is not null and (a.syncStatus<>1 and a.syncStatus<>3)
             and (a.status='".order_status_delivering."' or a.status='".order_status_submission."' or a.status='".ORDER_STATUS_TEAMING."')")
            ->field("a.*,b.supplierModule,b.authConfig")
            ->select();
        //a.wxTransactionId <> ''钱包支付没有wxTransactionId,此查询条件删除
        if(!$orderList||count($orderList)<=0){

            Output::instance()->resultCode(1,"没有可以同步的订单");
            return;
        }

        //$orderModel->where("a.id in ($oids) and b.supplierModule is not null and (a.syncStatus<>1 and a.syncStatus<>3) and a.wxTransactionId <> '' and a.status='".order_status_delivering."'")->setField("syncStatus",3);
        $orderModel->alias('a')->where("a.id in ($oids)  and (a.syncStatus<>1 and a.syncStatus<>3)
        and (a.status='".order_status_delivering."'or a.status='".order_status_submission."'
        or a.status='".ORDER_STATUS_TEAMING."')")->setField("syncStatus",3);

        $synchroRet = '';
        $taskId = $data['id'];
        foreach($orderList as $key=>$item){
            Session::switchCustomer(0);
            $orderModel->where("id=".$item['id'])->setField("syncStatus",3);
            $orderDetailModel = M("module_product_order_detail");
            $item['proList'] = $orderDetailModel->where("oid=".$item['id'])->select();
            $authConfig = json_decode($item['authConfig'],true);
            $appid = $authConfig['appid'];
            $appname = $authConfig['appname'];
            $appkey = $authConfig['appkey'];
            $synchroUrl = $authConfig['synchroUrl'];
            $disList = $authConfig['distribution'];
            if($item['supplierModule']=='weini'){
                foreach($disList as $key=>$val){
                    if($val['distributionId']==$item['distributionId']){
                        $item['deliveryCode'] = $val['deliveryCode'];
                    }
                }
                //Log::write('weini开始执行appid:'.$appid.'appkey:'.$appkey.'synchroUrl:'.$synchroUrl);
                $result = R("Weinihaigou/synchroOrder",array($item,$appid,$appkey,$synchroUrl));
                echo json_encode($result)."<br/>";
            }else if($item['supplierModule']=='kuajingmao'){
                $result = R("CrossBorderCat/synchroOrder",array($item,$appid,$appname,$appkey,$synchroUrl));
                echo json_encode($result)."<br/>";
                $data = null;
                if($result){
                    $data['syncStatus']=1;
                    $data['syncOrderSn'] = $result['order_sn'];
                    $data['syncTime'] = date("Y-m-d H:i:s");
                }else{
                    $data['syncStatus']=2;
                    $data['syncTime'] = date("Y-m-d H:i:s");
                }
                $data["syncMessage"] = $result['message'];
               self::updateOrder($item['serialNumber'],$data);
            }else if($item['supplierModule']=='EF'){
                $cid = $_GET['customerId'];
                $miContentTag_tab = M('micontent_tag_list');
                $miCustomerTag_tab = M('customer_tag_list');
                $miContent_tab = M('micontent');
                Session::switchCustomer($cid);
                $user_tab = M('user');
                Session::switchCustomer(0);

                $userInfo = $orderModel->alias('a')
                    ->join('wx_'.$cid.'_user b on b.id=a.uId')
                    ->join('wx_'.$cid.'_user_weixin_info e on e.userId=b.id')
                    ->join('wx_'.$cid.'_user c on c.id=a.miUid')
                    ->join('wx_'.$cid.'_user d on d.id=a.saleSource')
                    ->join('wx_customer_user f on d.userName=f.openid')
                    ->field('b.id,e.openid,e.unionid,c.phone as refereePhone,f.fullName as saleFullName')
                    ->where("a.id=".$item['id'])
                    ->find();

                /*$userInfo = $user_tab->alias('a')
                    ->join('wx_'.$cid.'_user_weixin_info i on a.id=i.userId')
                    ->field('a.id,i.openid,i.unionid')
                    ->where(['a.id'=>$item['uId']])
                    ->find();

                $userInfo['refereePhone'] = $user_tab->where(['id'=>$item['miUid']])->getField('phone');
                $userInfo['saleFullName'] = $user_tab->alias('a')
                    ->join('inner join wx_customer_user b on a.userName=b.openid')
                    ->where(['a.id'=>$item['saleSource'],'b.agent'=>'service','b.customerId'=>$cid])
                    ->getField('b.fullName');*/

                $userInfo['synchroUrl'] = $authConfig['synchroUrl'];
                $item['refereePhone'] = $userInfo['refereePhone'];
                $item['saleFullName'] = $userInfo['saleFullName'];
                if($item['type'] == ORDER_TYPE_FORM){   //营销订单
                    //文章分组改为完全按照销售的来
                    /*$groupInfo = $miContentTag_tab->alias('a')
                        ->join('wx_micontent_tag b on a.tagId=b.id')
                        ->join('wx_micontent_tag_group c on b.groupId=c.id')
                        ->where(['a.fromCustomerId'=>$cid,'a.webPageId'=>$item['moduleId'], 'c.deleted'=>['neq',1], 'b.deleted'=>['neq',1]])
                        ->field('c.id, c.groupName,b.tagName')
                        ->select();
                    if($groupInfo == null){*/
                        $groupInfo = $miCustomerTag_tab->alias('a')
                            ->join('wx_customer_tag b on a.tagId=b.id')
                            ->join('wx_customer_tag_group c on b.groupId=c.id')
                            ->join('inner join wx_customer_user d on a.uid=d.id')
                            ->join('inner join wx_'.$cid.'_user e on e.userName=d.openid')
                            ->where(['a.cid'=>$cid,'e.id'=>$item['saleSource'], 'c.deleted'=>['neq',1], 'b.deleted'=>['neq',1]])
                            ->field('c.id, c.groupName,b.tagName')
                            ->select();
                    //}
                    $CampaignAllocationProgram = $CampaignAllocationCode = $CampaignName =  '';
                    foreach($groupInfo as $key=>$val){
                        if($val['groupName'] == 'CampaignAllocationProgram'){
                            $CampaignAllocationProgram .= $val['tagName'].',';
                        }else if($val['groupName'] == 'CampaignAllocationCode'){
                            $CampaignAllocationCode .= $val['tagName'].',';
                        }else if($val['groupName'] == 'CampaignName'){
                            $CampaignName .= $val['tagName'];
                        }
                    }
                    $userInfo['CampaignAllocationProgram'] = trim($CampaignAllocationProgram,",");
                    $userInfo['CampaignAllocationCode'] = trim($CampaignAllocationCode,',');
                    $userInfo['CampaignName'] = $CampaignName;
                    $formSynchroRet = '';
                    foreach($item['proList'] as $proK=>$proV){
                        $formValInfo = json_decode($proV['customizedInfo'], true);
                        if($formValInfo){
                            foreach($formValInfo as $k=>$v){
                                /*a.fullName,a.phone,a.email,a.city,a.province,a.birthday*/
                                switch($v['type']){
                                    case 'name':
                                        $userInfo['fullName'] = $v['value'];
                                        break;
                                    case 'phone':
                                        $userInfo['phone'] = $v['value'];
                                        break;
                                    case 'date'://生日
                                        $userInfo['birthday'] = $v['value'];
                                        break;
                                    case 'city':
                                        $userInfo['city'] = $v['value']['city'];
                                        $userInfo['province'] = $v['value']['provice'];
                                        break;
                                    case 'email':
                                        $userInfo['email'] = $v['value'];
                                        break;
                                    case 'radio':
                                        switch(trim($v['value'])){
                                            case '中小学生海外游学团':
                                                $userInfo['CampaignAllocationProgram'] = 'LT';
                                                break;
                                            case '大学生及年轻白领海外游学':
                                                $userInfo['CampaignAllocationProgram'] = 'ILS';
                                                break;
                                            case '职场人士及成人海外游学':
                                                $userInfo['CampaignAllocationProgram'] = 'LSP';
                                                break;
                                            case '海外间隔年':
                                                $userInfo['CampaignAllocationProgram'] = 'LY';
                                                break;
                                            case '海外大学桥梁课程':
                                                $userInfo['CampaignAllocationProgram'] = 'BC';
                                                break;
                                        }

                                        $userInfo['CampaignAllocationCode'] = 'single';
                                        break;
                                }
                            }
                        }
                        $userInfo['articleName'] = $miContent_tab->where(['id'=>$proV['contentId']])->getField('title');
                        $userInfo['type'] = 'form';
                        $result = R("Ef/synchroFriends",[$userInfo]);
                        $formSynchroRet .= $result['synchroRet'].'|';
                    }
                    $result['synchroRet'] = $formSynchroRet;
                }else{
                    if($item['isTeam']){    //团购情况,发起人/总数/已团数
                        $DetailModel = M("module_team_order_detail");
                        $startPhone = $DetailModel->alias("a")
                            ->join("inner join wx_module_product_order b on a.oid = b.id")
                            ->join("inner join wx_module_product_order_detail c on b.id = c.oid")
                            ->field("c.customizedInfo")
                            ->where("a.teamId = ".$item['teamId']." and a.role = 1")->find();
                        $formStr = json_decode($startPhone['customizedInfo'], true);
                        $beginPhone = '';
                        foreach($formStr as $pKey=>$pVal){
                            //$val['name']需要和运营协商定死,如果单方面改动会导致失败
                            switch($pVal['name']){
                                case  '手机号码':    //手机
                                    $beginPhone = $pVal['description'];
                                    break;
                                default:
                                    break;
                            }
                        }
                        //Log::write('jie---teamOrder-'.$DetailModel->getLastSql());
                        $teamCount = $DetailModel->alias("a")
                            ->join("inner join wx_module_product_order_detail b on a.oid=b.oid")
                            ->join('wx_module_product c on c.id=b.pid')
                            ->join('wx_module_product_order d on a.oid=d.id')
                            ->field("c.teamCount")
                            ->where("a.teamId=".$item['teamId']." and (d.wxTransactionId <> '' or d.payType='delivery')")
                            ->select();
                        //Log::write('jie---teamOrder1-'.$DetailModel->getLastSql());
                        /*$teamCount = $orderModel->alias('a')
                            ->join('wx_module_product_order_detail b on a.id=b.oid')
                            ->join('wx_module_product c on b.pid=c.id')
                            ->join('wx_'.$cid.'_user d on a.uId=d.id')
                            ->where(['a.teamId'=>$item['teamId'],'a.fromCustomerId'=>$cid])
                            ->field('c.teamCount,d.phone')
                            ->select();*/

                        $item['teamFirstPhone'] = $beginPhone;
                        $item['teamInfo'] = count($teamCount).'/'.$teamCount[0]['teamCount'];
                    }
                    $result = R("Ef/synchroOrder",array($item));    //微商城订单
                }

                $data = null;
                $synchroRet .= 'orderId:'.$item['id'].',allRet['.$result['synchroRet'].']|';
                if($result['bool']){
                    $data['syncStatus']=1;
                    $data['syncOrderSn'] = $result['order_sn'];
                    $data['syncTime'] = date("Y-m-d H:i:s");
                }else{
                    $data['syncStatus']=2;
                    $data['syncTime'] = date("Y-m-d H:i:s");
                }
                $data["syncMessage"] = $result['synchroRet'];
                self::updateOrder($item['serialNumber'],$data);
            }
        }
        $synchroRet = trim($synchroRet, '|');
        $retSql = "UPDATE `wx_task_queue` SET  `result`='".$synchroRet."',`updateTime`='".date('Y-m-d H:i:s')."' WHERE (`id`=".$taskId.")";
        M()->execute($retSql);
        Output::instance()->resultCode();
        return true;
    }
    //更新订单同步结果
    public function updateOrder($orderNo,$data){
        Session::switchCustomer(0);
        $orderModel = M("module_product_order");
        $orderModel->where("serialNumber='$orderNo'")->save($data);
    }

    //自动库存同步接口
    public function autoSynchroStock()
    {
        Session::switchCustomer(0);
        $isAutoModel = M('customer');
        $where['autoSynchStock'] = 1;
        $cids = $isAutoModel->where($where)->field('id')->select();
        $this->synchroStocks($cids);
    }
    //批量库存  避免任务系统调用穿过来的参数误判成$id
    public function synchroStocks($cids = []){
        $retVal = Session::init();
        if ($retVal) {
        $cid = Session::$user['customerId'];
        self::synchroStock(null, false, $cid);
        } else {
            foreach($cids as $key => $value){
                $cid =$value['id'];
                self::synchroStock(null, false, $cid);
            }
        }
    }

    //商品库存同步
    public function synchroStock($id = null,$return = false,$cid = ''){
        Session::switchCustomer(0);
        $productModel = M("module_product");
        if($id){
            $proInfo = $productModel->alias("a")
                ->join("wx_mivendor b on a.vId=b.id")
                ->field("a.id,a.supplierSkuNo,b.supplierModule,b.authConfig")
                ->where("a.id=$id and b.supplierModule is not null")
                ->find();
                $authConfig = json_decode($proInfo['authConfig'],true);
//                if($proInfo['supplierModule']=='kuajingmao'){
//                    $result = R("CrossBorderCat/synchroStock",array($authConfig['appid'],$authConfig['appname'],$authConfig['appkey']));
//                    foreach($result as $key=>$item){
//                        $stock = $item['stock_num'];
//                        $_REQUEST['method'] = 'Differ.JH.Business.SyncStock';
//                        $bizcontent['platproductid'] = $proInfo['id'];
//                        $bizcontent['outerid'] = $proInfo['supplierSkuNo'];
//                        $bizcontent['quantity'] = $stock;
//                        $_REQUEST['bizcontent'] = json_encode($bizcontent);
//                        $bizcontent['returnType'] = 1;
//                        $_REQUEST['appkey']=$authConfig['platAppid'];
//                        $_REQUEST['token']=$authConfig['platToken'];
//                        $unsign     = $authConfig['platAppsecret'] . "appkey" . $authConfig['platAppid'] . "bizcontent" . json_encode($bizcontent) . "method" . 'Differ.JH.Business.SyncStock' . "token" . $authConfig['platToken'] . $authConfig['platAppsecret'];
//                        $sign    = md5(strtolower($unsign));
//                        $_REQUEST['sign']=$sign;
//                        $result = R("Boluopai/index");
//                        $_REQUEST = null;
//                    }
//                }else
                    if($proInfo['supplierModule']=='weini'){
                    $content['StockReqs']=[trim($proInfo['supplierSkuNo'])];
                    $result = R("Weinihaigou/synchroStock",array($content,$authConfig['appid'],$authConfig['appkey'],$authConfig['synchroUrl']));
                    if($result){
                        if (empty($result[0]['Quantity'])) {
                            $stock = 0;
                        } else {
                            $stock = $result[0]['Quantity'];
                        }
                        $_REQUEST['method'] = 'Differ.JH.Business.SyncStock';
                        $bizcontent['PlatProductID'] = $proInfo['id'];
                        $bizcontent['OuterID'] = trim($proInfo['supplierSkuNo']);
                        $bizcontent['Quantity'] = $stock;
                        $bizcontent['ReturnType'] = 1;
                        $_REQUEST['bizcontent'] = json_encode($bizcontent);
                        $_REQUEST['appkey']=$authConfig['platAppid'];
                        $_REQUEST['token']=$authConfig['platToken'];
                        $unsign     = $authConfig['platAppsecret'] . "appkey" . $authConfig['platAppid'] . "bizcontent" . json_encode($bizcontent) . "method" . 'Differ.JH.Business.SyncStock' . "token" . $authConfig['platToken'] . $authConfig['platAppsecret'];
                        $sign    = md5(strtolower($unsign));
                        $_REQUEST['sign']=$sign;
                        $result = R("Boluopai/index");
                        $_REQUEST = null;
                    }else{
                        Output::instance()->resultCode(1,"库存同步失败");
                        return;
                    }
                }
            if(!$return){
                Output::instance()->resultCode();
            }
        }else{
            $list = $productModel->alias("a")
                ->join("wx_mivendor b on a.vId=b.id")
                ->field("a.id,a.supplierSkuNo,b.supplierModule,b.authConfig,a.vId")
                ->where("a.deleted=0 and b.supplierModule is not null and a.fromCustomerId=$cid")
                ->select();
            $kjmVid = [];
            $weini = [];
            foreach($list as $key=>$item){
                $authConfig = json_decode($item['authConfig'],true);
//                if($item['supplierModule']=='kuajingmao'&&!$kjmVid[$item['vId']]){
//                    $result = R("CrossBorderCat/synchroStock",array($authConfig['appid'],$authConfig['appname'],$authConfig['appkey'], $authConfig['synchroUrl']));
//                    $kjmVid[$item['vId']] =$result;
//                }else
                if($item['supplierModule']=='weini'){
                    if(!$weini[$item['vId']]){
                        $weini[$item['vId']]['authConfig'] =$authConfig;
                    }
                    $weini[$item['vId']]['StockReqs'][] = trim($item['supplierSkuNo']);

                }
            }
            foreach($weini as $key=>$item){
                $data['StockReqs'] = $item['StockReqs'];
                $authConfig = $item['authConfig'];
                $result = R("Weinihaigou/synchroStock",array($data,$authConfig['appid'],$authConfig['appkey'],$authConfig['synchroUrl']));
                $weini[$key]['Quantity'] = $result;
            }
            echo json_encode($weini);
            $failCount = 0;
            $successCount = 0;
            foreach($list as $key=>$item){
                $authConfig = json_decode($item['authConfig'],true);
//                if($item['supplierModule']=='kuajingmao'){
//                    foreach($kjmVid[$item['vId']] as $i=>$k){
//                        if($k['goods_id']==$item['supplierSkuNo']){
//                            $_REQUEST['method'] = 'Differ.JH.Business.SyncStock';
//                            $bizcontent['platproductid'] = $item['id'];
//                            $bizcontent['outerid'] = $item['supplierSkuNo'];
//                            $stock = $k['stock_num'];
//                            $bizcontent['quantity'] = $stock;
//                            $bizcontent['returnType'] = 1;
//                            $_REQUEST['bizcontent'] = json_encode($bizcontent);
//                            $_REQUEST['appkey']=$authConfig['platAppid'];
//                            $_REQUEST['token']=$authConfig['platToken'];
//                            $unsign     = $authConfig['platAppsecret'] . "appkey" . $authConfig['platAppid'] . "bizcontent" . json_encode($bizcontent) . "method" . 'Differ.JH.Business.SyncStock' . "token" . $authConfig['platToken'] . $authConfig['platAppsecret'];
//                            $sign    = md5(strtolower($unsign));
//                            $_REQUEST['sign']=$sign;
//                            $result = R("Boluopai/index");
//                            $_REQUEST = null;
//                            if($result->code=='10000'){
//                                $successCount++;
//                            }else{
//                                $failCount++;
//                            }
//                        }
//                    }
//
//                }else
                    if($item['supplierModule']=='weini'){
                    foreach($weini[$item['vId']]['Quantity'] as $j=>$w){
                        if(trim($w['SkuNo'])==trim($item['supplierSkuNo'])){
                            $_REQUEST['method'] = 'Differ.JH.Business.SyncStock';
                            $bizcontent['PlatProductID'] = $item['id'];
                            $bizcontent['OuterID'] = trim($item['supplierSkuNo']);
                            $bizcontent['ReturnType'] = 1;
                            $stock = $w['Quantity'];
                            $bizcontent['Quantity'] = $stock;
                            $_REQUEST['bizcontent'] = json_encode($bizcontent);
                            $_REQUEST['appkey']=$authConfig['platAppid'];
                            $_REQUEST['token']=$authConfig['platToken'];
                            $unsign     = $authConfig['platAppsecret'] . "appkey" . $authConfig['platAppid'] . "bizcontent" . json_encode($bizcontent) . "method" . 'Differ.JH.Business.SyncStock' . "token" . $authConfig['platToken'] . $authConfig['platAppsecret'];
                            $sign    = md5(strtolower($unsign));
                            $_REQUEST['sign']=$sign;
                            $result = R("Boluopai/index");
                            $_REQUEST = null;
                            if($result['code']=='10000'){
                                $successCount++;
                            }else{
                                $failCount++;
                            }
                        }
                    }
                }
            }
            $result = null;
            $result['successCount'] = $successCount;
            $result['failCount'] = $failCount;
            Output::instance()->object($result);
            return true;
        }
    }
    //物流同步
    public function synchroLogistics($oid){
        Session::switchCustomer(0);
        $orderModel = M("module_product_order");
        $orderInfo = $orderModel->alias("a")
            ->join("wx_mivendor b on a.vId=b.id")
            ->where("a.id=$oid and a.status='".order_status_delivering."' and a.syncStatus=1")
            ->field("a.id,a.serialNumber,b.supplierModule,b.authConfig")
            ->find();
        if(!$orderInfo){
            Output::instance()->resultCode(1,'运单同步失败');
            return;
        }
        $authConfig = json_decode($orderInfo['authConfig'],true);
        if($orderInfo['supplierModule']=='weini'){
            $item['OrderNosReqs'] = [$orderInfo['serialNumber']];
            $data = R("Weinihaigou/synchroLogistics",array($item,$authConfig['appid'],$authConfig['appkey'],$authConfig['synchroUrl']));
        }else if($orderInfo['supplierModule']=='kuajingmao'){
            $item['orderNo']=$orderInfo['serialNumber'];
            $data = R("CrossBorderCat/synchroLogistics",array($item,$authConfig['appid'],$authConfig['appname'],$authConfig['appkey'],$authConfig['synchroUrl']));
        }
        if($data){
            if ($data['IsSend'] == 0) {
                Output::instance()->object([
                    'code' => 1,
                    'msg' => '订单未发货'
                ]);
                return;
            }
            $_REQUEST['method'] = 'Differ.JH.Business.Send';
            $bizcontent = $data;
            $bizcontent['PlatOrderNo'] = $orderInfo['serialNumber'];
            $bizcontent['resultType'] = 1;
            $_REQUEST['bizcontent'] = json_encode($bizcontent);
            $_REQUEST['appkey']=$authConfig['platAppid'];
            $_REQUEST['token']=$authConfig['platToken'];
            $unsign     = $authConfig['platAppsecret'] . "appkey" . $authConfig['platAppid'] . "bizcontent" . json_encode($bizcontent) . "method" . 'Differ.JH.Business.Send' . "token" . $authConfig['platToken'] . $authConfig['platAppsecret'];
            $sign    = md5(strtolower($unsign));
            $_REQUEST['sign']=$sign;
            $result = R("Boluopai/index");
            $_REQUEST = null;
            $result = json_decode($result,true);
            if($result['code']=='10000'){
                Output::instance()->resultCode();
            }else{
                Output::instance()->resultCode(1,'运单同步失败');
            }
        }else{
            Output::instance()->resultCode(1,'运单同步失败');
        }


    }

    /**
     * 同步好友
     * @param $taskInfo /任务信息
     */
    public function synchroFriends($taskInfo){
        /*Session::init();
        $cid = 3;
        $uids = json_decode($taskInfo, true)['param'];*/
        $cid =$_GET['customerId'];
        $uids = json_decode($taskInfo['param'], true);
        $user_tab = M('user');
        Session::switchCustomer(0);
        $userTag_tab = M('customer_tag_list');
        $quanUser_tab = M('quan_user');
        $taskQueue_tab = M('task_queue');
        Session::switchCustomer($cid);
        $synchroRet = '';
        foreach($uids as $k=>$v){
            $where = [];
            $where['a.id'] = $v['uid'];
            $where['c.id'] = $v['qId'];
            $userInfo = $user_tab->alias('a')
                ->join('wx_'.$cid.'_user_weixin_info i on a.id=i.userId')
                ->join('inner join wx_quan_user b on a.id=b.uid and b.deleted=0 and b.qid='.$v['qId'])
                ->join('inner join wx_miyouquan c on c.id=b.qid')
                ->join('wx_'.$cid.'_user d on b.refereeUid=d.id')
                ->join('wx_'.$cid.'_user e on e.id=b.saleSource')
                ->join("wx_mivendor f on f.id = c.vid")
                ->field('a.id,a.fullName,a.phone,a.email,a.city,a.province,a.birthday,i.openid,i.unionid,i.sex,
                b.id as quanUserId,b.saleSource,d.phone as refereePhone,e.fullName as saleFullName,f.authConfig')
                ->where($where)
                ->find();
            $authConfig = json_decode($userInfo['authConfig'], true);
            $userInfo['synchroUrl'] = $authConfig['synchroUrl'];
            //获取Etag
            $service = $user_tab->alias('a')
                ->join('inner join wx_customer_user b on a.userName=b.openid and b.agent="service" and b.customerId='.$cid)
                ->where(['a.id'=>$userInfo['saleSource']])
                ->field('b.id')
                ->find();
            Log::write('jie--tag'.$user_tab->getLastSql());
            $groupInfo = $userTag_tab->alias('a')
                ->join('wx_customer_tag b on a.tagId=b.id')
                ->join('wx_customer_tag_group c on b.groupId=c.id')
                ->where(['a.cid'=>$cid,'a.uid'=>$service['id'], 'c.deleted'=>['neq',1], 'b.deleted'=>['neq',1]])
                ->field('c.id, c.groupName, b.tagName')
                ->select();
            $CampaignAllocationProgram = $CampaignAllocationCode = $CampaignName =  '';
            foreach($groupInfo as $key=>$val){
                if($val['groupName'] == 'CampaignAllocationProgram'){
                    $CampaignAllocationProgram .= $val['tagName'].',';
                }else if($val['groupName'] == 'CampaignAllocationCode'){
                    $CampaignAllocationCode .= $val['tagName'].',';
                }else{
                    $CampaignName .= $val['tagName'];
                }
            }
            $userInfo['CampaignAllocationProgram'] = trim($CampaignAllocationProgram,",");
            $userInfo['CampaignAllocationCode'] = trim($CampaignAllocationCode,',');
            $userInfo['CampaignName'] = $CampaignName;
            if($userInfo['sex'] == 2){
                $userInfo['gender'] = 'F';
            }else{
                $userInfo['gender'] = 'M';
            }
            $quanUser_tab->where(['id'=>$userInfo['quanUserId']])->save(['syncStatus'=>3]);
            $result = R("Ef/synchroFriends",[$userInfo]);

            $data = null;
            $synchroRet .= $result['synchroRet'].'|';
            if($result['bool']){
                $data['syncStatus']=1;
                $data['syncOrderSn'] = $result['order_sn'];
                $data['syncTime'] = date("Y-m-d H:i:s");
            }else{
                $data['syncStatus']=2;
                $data['syncTime'] = date("Y-m-d H:i:s");
            }
            $data["syncMessage"] = $result['synchroRet'];
            $quanUser_tab->where(['id'=>$userInfo['quanUserId']])->save($data);
        }
        //更新任务结果
        $taskQueue_tab->where(['id'=>$taskInfo['id']])->save([
            'result'=>$synchroRet,
            'updateTime'=>date('Y-m-d H:i:s')
        ]);
    }
}